package com.teach;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class HttpServer {
	public static void main(String[] args)throws Exception {
		ServerSocket server = new ServerSocket(88);
		System.out.println("创建了服务器88");
		while (true) {
			Socket client = server.accept();
			InputStream in = client.getInputStream();
			BufferedReader request = new BufferedReader(new InputStreamReader(in,"UTF-8"));
			String line = null;
			while(!" ".equals(line=request.readLine())) {
				System.out.println(line);
			}
			System.out.println("读取完成");
			
			OutputStream out = client.getOutputStream();
			PrintWriter response = new PrintWriter(new OutputStreamWriter(out,"UTF-8"));
			response.println("HTTP/1.1 200 OK");
			response.println("Content-Type:text/html;charset=UTF-8");
			response.println();//空行后面是响应正文
			response.println("<h1>你好，这是我写的</h1>");
			response.flush();
			System.out.println("发送完成");
			
		}
		
	}
}
